﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SplashViewModel.cs" company="http://buginbox.wordpress.com/">
//   Copyright (c) GNU General Public License version 2 (GPLv2)
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

using System.ComponentModel.Composition;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.ServiceLocation;
using Wpf.Core.Event;
using Wpf.Core.ViewModel;

namespace Module.Splash.View
{
    /// <summary>
    /// Splash ViewModel
    /// </summary>
    [Export(typeof(SplashViewModel))]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class SplashViewModel : ViewModelBase
    {
        #region Constructor(s)

        /// <summary>
        /// Initializes a new instance of the <see cref="SplashViewModel"/> class.
        /// </summary>
        [ImportingConstructor]
        public SplashViewModel()
        {
            Status = string.Empty;

            var eventAggregator = ServiceLocator.Current.GetInstance<IEventAggregator>();
            eventAggregator.GetEvent<SplashMessageEvent>().Subscribe(e => UpdateMessage(e.Message));
        }

        #endregion

        #region Private Members

        /// <summary>
        /// The status
        /// </summary>
        private string _status;

        #endregion

        #region Public Members

        /// <summary>
        /// Gets or sets the status.
        /// </summary>
        public string Status
        {
            get
            {
                return _status;
            }

            set
            {
                _status = value;
                RaisePropertyChanged(() => Status);
            }
        }

        /// <summary>
        /// Gets or sets the splash view.
        /// </summary>
        /// <value>
        /// The splash view. 
        /// </value>
        public SplashView SplashView
        {
            get;
            set;
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Updates the message.
        /// </summary>
        /// <param name="message">
        /// The message. 
        /// </param>
        private void UpdateMessage(string message)
        {
            if (string.IsNullOrEmpty(message))
            {
                return;
            }

            Status += message;
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Inits the view.
        /// </summary>
        public void ShowView()
        {
            var splashView = new SplashView { DataContext = this };
            SplashView = splashView;
            SplashView.Show();
            SplashView.Activate();
        }

        #endregion
    }
}